const Error = require('./error')
const storage = require('./storage.js')
const request = require('./request')
const {
    isObject,
    isString,
    isNumber,
    isUndefined,
    isBoolean,
    isArray
} = require('./dataType.js')

const repairOrder = class repairOrder {

    constructor() {}

    get(id) {
        let params = {
            id: id
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.REPAIR_ORDER.GET.url,
                MS._config.parameters.REPAIR_ORDER.GET.method, params).then(res => {
                    resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    query(options) {
        let params = {
            pageSize: 1,
            pageNum: 10
        }
        if(!isUndefined(options)) {
            if(!isUndefined(options.moId) && isString(options.moId)) {
                params.moId = options.moId;
            }
            if(!isUndefined(options.applicantId) && isString(options.applicantId)) {
                params.applicantId = options.applicantId;
            }
            if(!isUndefined(options.status) && isString(options.status)) {
                params.status = options.status;
            }
            if(!isUndefined(options.statusArray) && isArray(options.statusArray)) {
                params.statusArray = JSON.stringify(options.statusArray);
            }
            if(!isUndefined(options.deviceId) && isString(options.deviceId)) {
                params.deviceId = options.deviceId;
            }
            if(!isUndefined(options.pageNum) && isNumber(options.pageNum)) {
                params.pageNum = options.pageNum;
            }
            if(!isUndefined(options.pageSize) && isNumber(options.pageSize)) {
                params.pageSize = options.pageSize;
            }
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.REPAIR_ORDER.QUERY.url,
                MS._config.parameters.REPAIR_ORDER.QUERY.method, params).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    apply(options) {
        var params = {}
        if(!isUndefined(options)) {
            if(!isUndefined(options.moId) && isString(options.moId)) {
                params.moId = options.moId;
            }
            if(!isUndefined(options.contacts) && isString(options.contacts)){
                params.contacts = options.contacts;
            }
            if(!isUndefined(options.contactsPhone) && isString(options.contactsPhone)){
                params.contactsPhone = options.contactsPhone;
            }
            if(!isUndefined(options.checkDeviceId) && isString(options.checkDeviceId)) {
                params.checkDeviceId = options.checkDeviceId;
            }
            if(!isUndefined(options.orderList) && isArray(options.orderList)) {
                params.orderListJson = JSON.stringify(options.orderList);
            }
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.REPAIR_ORDER.APPLY.url,
                MS._config.parameters.REPAIR_ORDER.APPLY.method, params).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    repairHandle(options) {
        var params = {}
        if(!isUndefined(options)) {
            if(!isUndefined(options.id) && isString(options.id)) {
                params.id = options.id;
            }
            if(!isUndefined(options.workPrice) && isNumber(options.workPrice)){
                params.workPrice = options.workPrice;
            }
            if(!isUndefined(options.deposit) && isNumber(options.deposit)){
                params.deposit = options.deposit;
            }
            if(!isUndefined(options.taskTime) && isNumber(options.taskTime)) {
                params.taskTime = options.taskTime;
            }
            if(!isUndefined(options.taskTimeUnit) && isNumber(options.taskTimeUnit)) {
                params.taskTimeUnit = options.taskTimeUnit;
            }
            if(!isUndefined(options.maintenanceGroupId) && isString(options.maintenanceGroupId)) {
                params.maintenanceGroupId = options.maintenanceGroupId;
            }
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.REPAIR_ORDER.HANDLE.url,
                MS._config.parameters.REPAIR_ORDER.HANDLE.method, params).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    distribution(options) {
        var params = {}
        if(!isUndefined(options)) {
            if(!isUndefined(options.id) && isString(options.id)) {
                params.id = options.id;
            }
            if(!isUndefined(options.maintenanceGroupId) && isString(options.maintenanceGroupId)) {
                params.maintenanceGroupId = options.maintenanceGroupId;
            }
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.REPAIR_ORDER.DISTRIBUTION.url,
                MS._config.parameters.REPAIR_ORDER.DISTRIBUTION.method, params).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    start(id) {
        var params = {
            id: id
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.REPAIR_ORDER.START.url,
                MS._config.parameters.REPAIR_ORDER.START.method, params).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    complete(options){
        var params = {}
        if(!isUndefined(options)) {
            if(!isUndefined(options.id) && isString(options.id)) {
                params.id = options.id;
            }
            if(!isUndefined(options.remark) && isString(options.remark)){
                params.remark = options.remark;
            }
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.REPAIR_ORDER.COMPLETE.url,
                MS._config.parameters.REPAIR_ORDER.COMPLETE.method, params).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    cancel(id) {
        var params = {
            id: id
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.REPAIR_ORDER.CANCEL.url,
                MS._config.parameters.REPAIR_ORDER.CANCEL.method, params).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    paySuccess(options) {
        var params = {}
        if(!isUndefined(options)) {
            if(!isUndefined(options.id) && isString(options.id)) {
                params.id = options.id;
            }
            if(!isUndefined(options.payPrice) && isNumber(options.payPrice)){
                params.payPrice = options.payPrice;
            }
            if(!isUndefined(options.payMode) && isNumber(options.payMode)){
                params.payMode = options.payMode;
            }
            if(!isUndefined(options.payMethod) && isNumber(options.payMethod)){
                params.payMethod = options.payMethod;
            }
            if(!isUndefined(options.payType) && isNumber(options.payType)){
                params.payType = options.payType;
            }
            if(!isUndefined(options.otherOrderId) && isNumber(options.otherOrderId)){
                params.otherOrderId = options.otherOrderId;
            }
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.REPAIR_ORDER.COMPLETE.url,
                MS._config.parameters.REPAIR_ORDER.COMPLETE.method, params).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }
}

module.exports = repairOrder